﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.VisualStyles;
using System.Windows.Forms.Design;

namespace Vibz.TeamAssignment
{
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ContextMenuStrip)]
    public class ToolStripMenuItemGrouped :ToolStripMenuItem
    {
        #region private members
        private int _groupIndex = 0;
        #endregion

        #region private methods
        /// <summary>
        /// Called by all constructors to initialize CheckOnClick. 
        /// </summary>
        private void Initialize()
        {
            CheckOnClick = true;
        }

        /// <summary>
        /// When the checked state of the parent item changes, 
        /// repaint the item so that the new Enabled state is displayed. 
        /// </summary>
        private void OwnerMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            Invalidate();
        }
        #endregion

        #region protected methods
        protected override void OnCheckedChanged(EventArgs e)
        {
            base.OnCheckedChanged(e);

            if((Parent == null) || (Parent.Items == null))
                return;

            // If this item is no longer in the checked state, do nothing.
            if(!Checked)
                return;

            // Clear the checked state for all siblings. 
            foreach(ToolStripItem item in Parent.Items)
                if(item is ToolStripMenuItemGrouped)
                {
                    ToolStripMenuItemGrouped radioItem = item as ToolStripMenuItemGrouped;
                    if((radioItem != null) && (radioItem != this) && (radioItem.Checked) && (radioItem.GroupIndex == _groupIndex))
                    {
                        radioItem.Checked = false;

                        // Only one item can be selected at a time, 
                        // so there is no need to continue.
                        return;
                    }
                }
        }
        protected override void OnClick(EventArgs e)
        {
            // If the item is already in the checked state, do not call 
            // the base method, which would toggle the value. 
            if(!Checked)
                base.OnClick(e);
        }

        /// <summary>
        /// When OwnerItem becomes available, if it is a ToolStripMenuItem 
        /// with a CheckOnClick property value of true, subscribe to its 
        /// CheckedChanged event. 
        /// </summary>
        protected override void OnOwnerChanged(EventArgs e)
        {
            ToolStripMenuItem ownerMenuItem =
                OwnerItem as ToolStripMenuItem;
            if(ownerMenuItem != null && ownerMenuItem.CheckOnClick)
            {
                ownerMenuItem.CheckedChanged +=
                    new EventHandler(OwnerMenuItem_CheckedChanged);
            }
            base.OnOwnerChanged(e);
        }
        #endregion

        #region constructor
        public ToolStripMenuItemGrouped()
            : base()
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(string text)
            : base(text, null, (EventHandler)null)
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(Image image)
            : base(null, image, (EventHandler)null)
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(string text, Image image)
            : base(text, image, (EventHandler)null)
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(string text, Image image,
            EventHandler onClick)
            : base(text, image, onClick)
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(string text, Image image,
            EventHandler onClick, string name)
            : base(text, image, onClick, name)
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(string text, Image image,
            params ToolStripItem[] dropDownItems)
            : base(text, image, dropDownItems)
        {
            Initialize();
        }

        public ToolStripMenuItemGrouped(string text, Image image,
            EventHandler onClick, Keys shortcutKeys)
            : base(text, image, onClick)
        {
            Initialize();
            this.ShortcutKeys = shortcutKeys;
        }
        #endregion

        #region public properties
        /// <summary>
        /// Enable the item only if its parent item is in the checked state 
        /// and its Enabled property has not been explicitly set to false.  
        /// </summary>
        public override bool Enabled
        {
            get
            {
                ToolStripMenuItem ownerMenuItem =
                    OwnerItem as ToolStripMenuItem;

                // Use the base value in design mode to prevent the designer
                // from setting the base value to the calculated value.
                if(!DesignMode &&
                    ownerMenuItem != null && ownerMenuItem.CheckOnClick)
                {
                    return base.Enabled && ownerMenuItem.Checked;
                }
                else
                    return base.Enabled;
            }
            set
            {
                base.Enabled = value;
            }
        }
        public int GroupIndex
        {
            get
            {
                return _groupIndex;
            }
            set
            {
                _groupIndex = value;
            }
        }
        #endregion
    }
}